﻿using System;
using System.Data;
using System.Collections.Generic;
using BYFW.DAL;
using BYFW.BOL;

namespace LDSP.BRL
{
    public partial class SearchKeywords : BYFW.BOL.BaseEntity
    {
        public SearchKeywords()
        {
            m_tableName = "t_searchKeywords";
            m_tblCacheManager.IsEnable = true;
        }

        protected IDBOP DefaultDB
        {
            get { return new BaseEntity().DB; }
        }

        protected string m_keywords = string.Empty;
        protected int m_count = 0;
        protected string m_page = string.Empty;

        /// <summary>
        /// 关键字
        /// </summary>
        public string Keywords
        {
            get { return m_keywords; }
            set { m_keywords = value; }
        }
        /// <summary>
        /// 次数
        /// </summary>
        public int Count
        {
            get { return m_count; }
            set { m_count = value; }
        }
        /// <summary>
        /// 页面名称
        /// </summary>
        public string Page
        {
            get { return m_page; }
            set { m_page = value; }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("keywords", m_keywords);
            m_container.Add("count", m_count);
            m_container.Add("page", m_page);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_keywords = this.GetStringValue(record, "keywords");
            m_count = this.GetIntValue(record, "count");
            m_page = this.GetStringValue(record, "page");
            return true;
        }

        public List<string> GetTop10Words(string prefixText)
        {
            string cond = "where " + DefaultDB.GetSQLNormalCondition("keywords", EDataType.String, prefixText + "%", "like", true);
            DataTable dt = this.GetRecords(cond);
            List<String> suggArList = new List<string>();

            if (dt != null && dt.Columns.Count >0)
            {
                DataRow[] rows = dt.Select("", "count DESC");
                int total = rows.Length > 10 ? 10 : rows.Length;
                for (int i = 0; i < total; i++)
                {
                    SearchKeywords keyword = new SearchKeywords();
                    keyword.GetProperties(rows[i]);
                    suggArList.Add(keyword.Keywords);
                }
            }

            return suggArList;
        }

        public void IncreaseWordsCount(string prefixText)
        {
            string cond = "where " + DefaultDB.GetSQLNormalCondition("keywords", EDataType.String, prefixText, "=", true);
            DataTable dt = this.GetRecords(cond);

            //若存在，则count加1.
            SearchKeywords searchKeywords = new SearchKeywords();
            if (dt != null && dt.Rows.Count > 0)
            {
                searchKeywords.GetProperties(dt.Rows[0]);
                searchKeywords.Count++;
                searchKeywords.Update();
            }
            else
            {
                //若不存在，则添加一条，并count设为1.
                searchKeywords.Keywords = prefixText;
                searchKeywords.Count = 1;
                searchKeywords.Add();
            }
        }
    }
}